/**
 * Created by Mark_ren on 2017/5/23.
 * 今天心情不好，可能是因为阴天。
 */
var moment = require("moment");
var co = require('co');
var seqModule = require('../../../routes/createModules');
var seq = new seqModule();
var util = require('../../../common/util');

var integralRecordController = {
    /**
     *  增加积分记录
     * @param req
     * @param res
     */
    addIntegralRecord: function (req, res) {
        co(function () {
            req.query.id = util.getUUID();
            req.query.createTime = util.getNowTime();
            var memberId = req.query.memberId;
            var filed1 = req.query.filed1;
            var params = {};

            var filed10 = parseInt(req.query.filed10);
            var score = parseInt(req.query.score);
            var jfState = parseInt(req.query.jfState);
            seq.integralRecordModule.count({ where: { memberId: memberId, filed1: filed1 } }).then(function (scoreCount) {
                if (scoreCount == 0) {
                    if (jfState == 2 && filed10 < score) {
                        res.json({
                            success: false,
                            msg: "积分不足！"
                        });
                    } else if (jfState == 1 || (jfState == 2 && filed10 >= score)) {
                        if (jfState == 1) {
                            filed10 += score;
                        } else if (jfState == 2) {
                            filed10 -= score;
                        }
                        params.filed10 = filed10;
                        delete req.query.filed10;
                        req.query.jfType = parseInt(req.query.jfType);
                        req.query.jfState = parseInt(req.query.jfState);

                        return seq.sequelize.transaction(function (t) {
                            return seq.integralRecordModule.create(req.query, { transaction: t }).then(function (integralRecord) {
                                return seq.memberModule.update(params, { where: { id: memberId } }, { transaction: t }).then(function (memberData) {
                                    res.json({
                                        "success": true,
                                        "msg": "添加成功！",
                                        "result": integralRecord
                                    });
                                });
                            });
                        }).catch(function (err) {
                            res.json({
                                "success": false,
                                "msg": "服务异常！"
                            });
                        });



                    } else {
                        res.json({
                            success: false,
                            msg: "参数不对！"
                        });
                    }

                } else {
                    res.json({
                        "success": true,
                        "msg": "已查看"
                    });
                }
            })


        }).catch(function (e) {
            res.json({ 'success': false, 'msg': '新增失败' });
            console.log(e);
        })

    }
}
module.exports = integralRecordController;